﻿using System;
using System.Diagnostics;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using HYCodeBase.WinForm;

namespace HangerSupport
{
	public class HSManage
	{
		public HSManage(UIApplication uiApp)
		{
			bool flag = HSManage._hWndRevit == null;
			if (flag)
			{
				Process currentProcess = Process.GetCurrentProcess();
				IntPtr mainWindowHandle = currentProcess.MainWindowHandle;
				HSManage._hWndRevit = new WindowHandle(mainWindowHandle);
			}
			this.m_uiApp = uiApp;
			this.m_doc = this.m_uiApp.ActiveUIDocument.Document;
			this.m_selection = this.m_uiApp.ActiveUIDocument.Selection;
		}

		public bool DesignHS()
		{
			PipeSelect pipeSelect = new PipeSelect(this.m_uiApp);
			Level level = null;
			string text = "";
			bool flag = this.m_uiApp.ActiveUIDocument.Document != null;
			if (flag)
			{
				level = this.m_uiApp.ActiveUIDocument.ActiveView.GenLevel;
			}
			bool flag2 = level != null;
			if (flag2)
			{
				text = level.Name;
			}
			HSDraw hsdraw = new HSDraw(this.m_uiApp, ref HSManage.m_HSTypeManage);
			bool flag3 = false;
			for (;;)
			{
				bool flag4 = text != HSManage.m_HSTypeManage.m_hsTypeDefault.m_strElevationName && level != null;
				if (flag4)
				{
					HSManage.m_HSTypeManage.m_hsTypeDefault.m_strElevationName = text;
				}
				DlgHSDesign dlgHSDesign = new DlgHSDesign(ref HSManage.m_HSTypeManage, ref pipeSelect, this.m_uiApp, flag3);
				bool flag5 = DialogResult.OK != dlgHSDesign.ShowDialog(HSManage._hWndRevit);
				if (flag5)
				{
					break;
				}
				bool flag6 = flag3;
				if (flag6)
				{
					flag3 = false;
				}
				HSManage.m_HSTypeManage.m_hsTypeDefault = dlgHSDesign.GetSelHS();
				string strElevationName = HSManage.m_HSTypeManage.m_hsTypeDefault.m_strElevationName;
				HSManage.m_HSTypeManage.SetItem(HSManage.m_HSTypeManage.m_hsTypeDefault);
				HSDefineInfo hsTypeDefault = null;
				hsTypeDefault = new HSDefineInfo(HSManage.m_HSTypeManage.m_hsTypeDefault);
				switch (dlgHSDesign.m_kDrawType)
				{
				case HSDrawType.eDrawTypeSelectSection:
				{
					bool flag7 = HSManage.m_HSTypeManage.m_hsTypeDefault.m_eType == HSType.kZJ_CB || HSManage.m_HSTypeManage.m_hsTypeDefault.m_eType == HSType.kDJ_DG;
					if (flag7)
					{
						pipeSelect.SelectSinglePipeAll(HSManage.m_HSTypeManage.m_hsTypeDefault.m_eType == HSType.kZJ_CB, "请选择风管、管道、桥架");
					}
					else
					{
						pipeSelect.SelectPipe(false);
					}
					flag3 = true;
					break;
				}
				case HSDrawType.eDrawTypeDrawSingle:
					hsdraw.DrawSingleFree();
					break;
				case HSDrawType.eDrawTypeDrawByPipe:
					hsdraw.DrawByPipe(pipeSelect, !pipeSelect.IsNoSelect(), HSDrawType.eDrawTypeDrawByPipe);
					break;
				case HSDrawType.eDrawTypeDrawByCurve:
				{
					bool flag8 = hsdraw.DrawByCurve();
					break;
				}
				case HSDrawType.eDrawTypeDrawBySectionPipe:
				{
					bool flag9 = pipeSelect.IsNoSelect();
					if (flag9)
					{
						Utils.MessageBox("没有进行剖面提取，按任意布置进行");
						hsdraw.DrawSingleFree();
					}
					else
					{
						hsdraw.DrawSingleBySectionPipe(pipeSelect);
					}
					break;
				}
				case HSDrawType.eDrawTypeDrawBy2Pipe:
					hsdraw.DrawBy2Pipe();
					break;
				case HSDrawType.eDrawTypeGetDefFromDraw:
					hsdraw.GetDefineFromDrawSelect(ref hsTypeDefault);
					break;
				case HSDrawType.eDrawTypeApplyToSelect:
					hsdraw.ApplyDefineToSelect();
					break;
				case HSDrawType.eDrawTypeDrawByWall:
				{
					bool flag10 = hsdraw.DrawByWall(pipeSelect);
					if (flag10)
					{
						goto Block_11;
					}
					break;
				}
				case HSDrawType.eDrawTypeDrawBySectionLine:
					hsdraw.DrawBySectionLine(pipeSelect, flag3, HSDrawType.eDrawTypeDrawBySectionLine);
					break;
				}
				HSManage.m_HSTypeManage.m_hsTypeDefault = hsTypeDefault;
			}
			return true;
			Block_11:
			return true;
		}

		private UIApplication m_uiApp;

		private Document m_doc;

		private Selection m_selection;

		private static HSTypeInfo m_HSTypeManage = new HSTypeInfo();

		public static WindowHandle _hWndRevit = null;
	}
}
